

grstyle clear

grstyle init
grstyle set plain, box
grstyle color background white
grstyle set color gs10%50
grstyle yesno draw_major_hgrid yes
grstyle yesno draw_major_ygrid yes
set scheme s1mono

	clear all
	set more off
	numlabel, add
	
 cd "$data"
use "dataset.dta", clear 

corr selfplacement redistribution regulation  samesexmarriage immigration environment laworder euintegration
 
factor selfplacement redistribution regulation  samesexmarriage immigration environment laworder euintegration
*screeplot, title("fdf")

rotate, promax



**** get the scree plots for each survey 

use "dataset.dta", clear
 cd "${data}/factors"
 
 
 
forvalues c = 1/57 {
    local cname : label (country_year_) `c'
    factor selfplacement redistribution regulation ///
           samesexmarriage immigration environment ///
           laworder euintegration if country_year_==`c'
    screeplot, title("`cname'") ytitle("") xtitle("")
	graph save "eigenvalues_`c'.gph", replace
}
 

graph combine eigenvalues_1.gph eigenvalues_2.gph eigenvalues_3.gph eigenvalues_4.gph eigenvalues_5.gph eigenvalues_6.gph eigenvalues_7.gph eigenvalues_8.gph eigenvalues_9.gph eigenvalues_10.gph eigenvalues_11.gph eigenvalues_12.gph eigenvalues_13.gph eigenvalues_14.gph eigenvalues_15.gph eigenvalues_16.gph eigenvalues_17.gph eigenvalues_18.gph eigenvalues_19.gph eigenvalues_20.gph eigenvalues_21.gph eigenvalues_22.gph eigenvalues_23.gph eigenvalues_24.gph eigenvalues_25.gph eigenvalues_26.gph eigenvalues_27.gph eigenvalues_28.gph eigenvalues_29.gph, ycommon
	
	graph save "${figures}/eigenvalues_merged_1.gph", replace
	graph export "${figures}/eigenvalues_merged_1.png", as(png) replace

graph combine eigenvalues_30.gph eigenvalues_31.gph eigenvalues_32.gph eigenvalues_33.gph eigenvalues_34.gph eigenvalues_35.gph eigenvalues_36.gph eigenvalues_37.gph eigenvalues_38.gph eigenvalues_39.gph eigenvalues_40.gph eigenvalues_41.gph eigenvalues_42.gph eigenvalues_43.gph eigenvalues_44.gph eigenvalues_45.gph eigenvalues_46.gph eigenvalues_47.gph eigenvalues_48.gph eigenvalues_49.gph eigenvalues_50.gph eigenvalues_51.gph eigenvalues_52.gph eigenvalues_53.gph eigenvalues_54.gph eigenvalues_55.gph eigenvalues_56.gph eigenvalues_57.gph, ycommon
	
	graph save "${figures}/eigenvalues_merged_2.gph", replace
	graph export "${figures}/eigenvalues_merged_2.png", as(png) replace


	
	*===============*
	* create factors for each survey and then run the regressions
	
cd "${data}"
use "dataset.dta", clear
	
	// Create empty placeholders
gen factor1 = .
gen factor2 = .
*gen factor3 = .

forvalues c = 1/57 {
    local cname : label (country_year_) `c'
    factor selfplacement redistribution regulation ///
           samesexmarriage immigration environment ///
           laworder euintegration if country_year_==`c', factors(2)
    rotate, promax
	
    predict f1 f2 
    replace factor1 = f1 if country==`c'
    replace factor2 = f2 if country==`c'
    *replace factor3 = f3 if country==`c'

    drop f1 f2 
}


	 
	eststo M1: regress factor1 voted  i.country_year_ if samplelimit==1, robust cluster(country_)
	
	eststo M2: regress factor2 voted  i.country_year_ if samplelimit==1, robust cluster(country_)

	
 esttab M1 M2 using ${tables}/factors.tex, replace ///
 b(3) se(2)  ///
 collabels("factor 1" "factor 2" ) ///
 coeflabels(intercept "label of intercept") ///
 title("") 

 
	save "dataset_factor.dta", replace

	
	
	*******************************************
	
	
	
	cd "${data}"
	use "dataset_factor.dta", clear 
	
	
	
drop if factor1==.
drop if voted==.
*gen samplelimit = 1 if age!=. & education!=. & welloff!=. & gender!=.

		
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted using ${data}/factors/factor1.dta, replace 

forvalues i = 1/57 {
    noisily regress factor1 voted if country_year_==`i' & samplelimit==1, robust 
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))
    post `turnout_file' (`i') (`=_b[voted]') (`=_se[voted]') (`=p_value')
}

postclose `turnout_file'

use ${data}/factors/factor1.dta, clear

histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black))  xtitle("") title("factor 1") ytitle("")

graph save "${data}/factors/pvalues_factor1.gph", replace 

	
	
	
cd "${data}"
	use "dataset_factor.dta", clear 

drop if factor2==.
drop if voted==.
*gen samplelimit = 1 if age!=. & education!=. & welloff!=. & gender!=.

		
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted using ${data}/factors/factor2.dta, replace 

forvalues i = 1/57 {
    noisily regress factor2 voted if country_year_==`i' & samplelimit==1, robust 
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))
    post `turnout_file' (`i') (`=_b[voted]') (`=_se[voted]') (`=p_value')
}

postclose `turnout_file'

use ${data}/factors/factor2.dta, clear

histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black))  xtitle("") title("factor 2") ytitle("")

graph save "${data}/factors/pvalues_factor2.gph", replace 

cd "${data}/factors"
graph combine "pvalues_factor1.gph" "pvalues_factor2.gph", ycommon 
graph save "${figures}/pvalues_factors_merged.gph", replace 
graph export "${figures}/pvalues_factors_merged.png", as(png) replace

	
	*****************************************************
				* cohen's d factor 1 *
	*****************************************************
	
	
cd "${data}"
use "dataset_factor.dta", clear
 

	 keep country_year_ factor1 voted  
	 bysort country_year_: egen mean_voter = mean(factor1) if voted==1
	 bysort country_year_: egen mean_nonvoter = mean(factor1) if voted==0
	 bysort country_year_: egen sd_ = sd(factor1) 
     drop factor1 voted
	 duplicates drop 
	 collapse (mean) mean_voter mean_nonvoter sd_, by(country_year_) 
	 gen difference = abs(mean_voter - mean_nonvoter)
	 gen cohend = difference / sd
	 cd "${data}/factors"
	 
	 tostring country_year_, generate(country_year)
	destring country_year, generate(country_year__)
	drop country_year_ country_year 
	rename country_year__ sample
	
	 save "factor1_cohen1.dta", replace 
	
	
	*** cohen d figure factor 1
	
	cd "${data}/factors"
	use "factor1_cohen1.dta", clear 
	merge 1:1 sample using "${data}/factors/factor1.dta"
	drop if _merge==1

	

	
	
drop if mean_voter==.
gen significant = .
replace significant = 1 if p_voted<0.06
replace significant = 0 if p_voted>0.05


  dotplot cohend, center xlabel(, angle(90) labsize(small)) xtitle("") yscale(range(0(0.2)0.6)) ylabel(0(0.2)0.6) yline(0.2, lpattern(dash) lcolor(black)) mcolor(gs9%30) msize(small) jitter(2) ysize(6) xsize(8) yline(0.5) colorvar(significant) colorrule(%25) colordiscrete coloruseplegend colorlist(gs12%50 black%70)

  graph save "cohendsd_factor1.gph", replace 

  
  
	
	
** cohen's d factor 2


cd "${data}"
use "dataset_factor.dta", clear
 

	 keep country_year_ factor2 voted  
	 bysort country_year_: egen mean_voter = mean(factor2) if voted==1
	 bysort country_year_: egen mean_nonvoter = mean(factor2) if voted==0
	 bysort country_year_: egen sd_ = sd(factor2) 
     drop factor2 voted
	 duplicates drop 
	 collapse (mean) mean_voter mean_nonvoter sd_, by(country_year_) 
	 gen difference = abs(mean_voter - mean_nonvoter)
	 gen cohend = difference / sd
	 cd "${data}/factors"
	 
	 tostring country_year_, generate(country_year)
	destring country_year, generate(country_year__)
	drop country_year_ country_year 
	rename country_year__ sample
	
	
	 save "factor2_cohen1.dta", replace 
	
	
	*** cohen d figure factor 1
	
	cd "${data}/factors"
	use "factor2_cohen1.dta", clear 
	merge 1:1 sample using "${data}/factors/factor2.dta"
	drop if _merge==1

	
	
drop if mean_voter==.
gen significant = .
replace significant = 1 if p_voted<0.06
replace significant = 0 if p_voted>0.05


  dotplot cohend, center xlabel(, angle(90) labsize(small)) xtitle("") yscale(range(0(0.2)0.6)) ylabel(0(0.2)0.6) yline(0.2, lpattern(dash) lcolor(black)) mcolor(gs9%30) msize(small) jitter(2) ysize(6) xsize(8) yline(0.5) colorvar(significant) colorrule(%25) colordiscrete coloruseplegend colorlist(gs12%50 black%70)

  graph save "cohendsd_factor2.gph", replace 
  
  
  graph combine "cohendsd_factor1.gph" "cohendsd_factor2.gph", ycommon
  graph save "${figures}/cohendsd_factor_merged.gph", replace 
  graph export "${figures}/cohendsd_factor_merged.png", as(png) replace

  
	
	*****************************************************
						* BIAS *
	*****************************************************
	
* factor 1
	
cd "${data}"
use "dataset_factor.dta", clear

keep country_year_ factor1 voted turnout_r 
 
bysort country_year_: egen mean_voter = mean(factor1) if voted==1
bysort country_year_: egen mean_nonvoter = mean(factor1) if voted==0
gen proportion_nonvoter = 1 - turnout_r
gen mean_voter_w = mean_voter * turnout_r
gen mean_nonvoter_w = mean_nonvoter * proportion_nonvoter
drop factor1 voted
duplicates drop 

drop if mean_voter==. & mean_nonvoter==.

by country_year_: replace mean_voter        = mean_voter[_n+1]        if missing(mean_voter)
by country_year_: replace mean_nonvoter     = mean_nonvoter[_n+1]     if missing(mean_nonvoter)
by country_year_: replace mean_voter_w      = mean_voter_w[_n+1]      if missing(mean_voter_w)
by country_year_: replace mean_nonvoter_w   = mean_nonvoter_w[_n+1]   if missing(mean_nonvoter_w)

by country_year_: keep if _n==1

gen outcome = mean_voter_w + mean_nonvoter_w
gen bias =  outcome - mean_voter

 tostring country_year_, generate(country_year)
 destring country_year, generate(sample)
 drop if sample==.
 
	merge 1:1 sample using "${data}/factors/factor1.dta"
	drop if _merge==1
	
 gen significant = .
replace significant = 1 if p_voted<0.06
replace significant = 0 if p_voted>0.05

gen bias2 = abs(bias)
scatter bias2 turnout_r, mcolor(black%60) msize(4pt) jitter(2) colorvar(significant) colorrule(%25) colordiscrete coloruseplegend colorlist(gs12%40 black%80) legend(off) title("factor 1") ytitle("bias") xtitle("reported turnout")

  graph save "${data}/factors/bias_factor1.gph", replace 

  
  dotplot bias, center xlabel(, angle(90) labsize(small)) xtitle("") yscale(range(-0.08(0.04)0.08)) ylabel(-0.08(0.04)0.08) yline(0, lpattern(dash) lcolor(black)) mcolor(gs9%30) msize(small) jitter(2) ysize(6) xsize(8) yline(0.5) colorvar(significant) colorrule(%25) colordiscrete coloruseplegend colorlist(gs12%50 black%70) xlabel("")
	
   graph save "${data}/factors/fullbias_factor1.gph", replace 


* factor 2
	  
cd "${data}"
use "dataset_factor.dta", clear
	  
keep country_year_ factor2 voted turnout_r 
 
bysort country_year_: egen mean_voter = mean(factor2) if voted==1
bysort country_year_: egen mean_nonvoter = mean(factor2) if voted==0
gen proportion_nonvoter = 1 - turnout_r
gen mean_voter_w = mean_voter * turnout_r
gen mean_nonvoter_w = mean_nonvoter * proportion_nonvoter
drop factor2 voted
duplicates drop 
drop if mean_voter==. & mean_nonvoter==.

by country_year_: replace mean_voter        = mean_voter[_n+1]        if missing(mean_voter)
by country_year_: replace mean_nonvoter     = mean_nonvoter[_n+1]     if missing(mean_nonvoter)
by country_year_: replace mean_voter_w      = mean_voter_w[_n+1]      if missing(mean_voter_w)
by country_year_: replace mean_nonvoter_w   = mean_nonvoter_w[_n+1]   if missing(mean_nonvoter_w)

by country_year_: keep if _n==1

gen outcome = mean_voter_w + mean_nonvoter_w
gen bias =  outcome - mean_voter

 tostring country_year_, generate(country_year)
 destring country_year, generate(sample)
 drop if sample==.
 
	merge 1:1 sample using "${data}/factors/factor2.dta"
	drop if _merge==1
	
 gen significant = .
replace significant = 1 if p_voted<0.06
replace significant = 0 if p_voted>0.05

gen bias2 = abs(bias)
scatter bias2 turnout_r, mcolor(black%60) msize(4pt) jitter(2) colorvar(significant) colorrule(%25) colordiscrete coloruseplegend colorlist(gs12%40 black%80) legend(off) title("factor 2") ytitle("bias") xtitle("reported turnout")

  graph save "${data}/factors/bias_factor2.gph", replace 

  cd "${data}/factors"
  graph combine "bias_factor1.gph" "bias_factor2.gph", ycommon 
	  graph save "${figures}/bias_factors_merged.gph", replace 
	  graph export "${figures}/bias_factors_merged.png", as(png) replace


  
    dotplot bias, center xlabel(, angle(90) labsize(small)) xtitle("") yscale(range(-0.08(0.04)0.08)) ylabel(-0.08(0.04)0.08) yline(0, lpattern(dash) lcolor(black)) mcolor(gs9%30) msize(small) jitter(2) ysize(6) xsize(8) yline(0.5) colorvar(significant) colorrule(%25) colordiscrete coloruseplegend colorlist(gs12%50 black%70) xlabel("")
	
	  graph save "${data}/factors/fullbias_factor2.gph", replace 


	  
  graph combine "fullbias_factor1.gph" "fullbias_factor2.gph", ycommon 
	  graph save "${figures}/fullbias_merged.gph", replace 
	  graph export "${figures}/fullbias_merged.png", as(png) replace


	
	
	
	
	
	
	
	